//
//  HomeTableViewController.swift
//  weibo
//
//  Created by 陈经纬 on 16/7/18.
//  Copyright © 2016年 陈经纬. All rights reserved.
//

import UIKit
private let bt = titleButton()
class HomeTableViewController: BaseTableViewController {

    override func viewDidLoad() {
        if isLogin == false {
            visitView?.setupVisitData("关注一些人.你会很高兴", imageName: nil)
        }
        
        //添加导航条上的左右按钮
//        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigationbar_friendattention"), style: UIBarButtonItemStyle.Plain, target: self, action: #selector(HomeTableViewController.leftBarButtonClick))
//        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigationbar_pop"), style: UIBarButtonItemStyle.Plain, target: self, action: #selector(HomeTableViewController.rightBarButtonClick))
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: "navigationbar_friendattention",  target: self , action:#selector(HomeTableViewController.leftBarButtonClick) )
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: "navigationbar_pop",  target: self , action:#selector(HomeTableViewController.rightBarButtonClick) )
        
        //自定义导航条上的标题按钮
//        let titleBtn = titleButton()
        bt.setTitle("经纬在前进", forState: UIControlState.Normal)
        bt.addTarget(self, action: #selector(HomeTableViewController.titleBtnCilck), forControlEvents: UIControlEvents.TouchUpInside)

        navigationItem.titleView = bt
        
        
        //注册通知,解决弹出菜单点击其他位置,虽然弹出的菜单能收回,但是标题按钮的图片不会回去,只有点击标题按钮才回去这个BUG
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.titleBtnChange), name: CJWNoticationTitleImageUp, object: nil)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.titleBtnChange), name: CJWNoticationTitleImageDown, object: nil)
        
    }
    //执行通知方法
    @objc private func titleBtnChange(){
        bt.selected = !bt.selected
    }
    
    
    
    /**
     点击左按钮事件 private代表私有
     */
    @objc private func leftBarButtonClick(){
        JWLog("")
    }
    /**
     点击右按钮事件
     */
    @objc private func rightBarButtonClick() {
        let qrVC = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        
        let qrPresent = qrVC.instantiateInitialViewController()
        presentViewController(qrPresent!, animated: true, completion: nil)
    }
    
    /**
     点击标题事件
     */
    @objc private func titleBtnCilck(){
//        bt.selected = !bt.selected
        
//        //modal出小的列表控制器
//        let pop = UIStoryboard(name: "popPresentationControl", bundle: nil)
//        guard let menuView = pop.instantiateInitialViewController() else
//        {
//            return
//        }
//        
//        //自定义转场动画
//        //设置转场代理
//        menuView.transitioningDelegate = self
//        
//        menuView.modalPresentationStyle = UIModalPresentationStyle.Custom
        let pop = popPresentationControl.popVC()
        
        
        
        //自定义转场动画
        //设置转场代理
        //代码优化:把self用新建的类包装起来,把所有的pop代理方法在新建类实现,需要懒加载新建的类
        pop.transitioningDelegate = popManager

        pop.modalPresentationStyle = UIModalPresentationStyle.Custom

        presentViewController(pop, animated: true, completion: nil)
        
    }
    
    //代码优化:把self用新建的类包装起来,把所有的pop代理方法在新建类实现,需要懒加载新建的类
    lazy var popManager: CJWPoprensionManger = {
        let popManger = CJWPoprensionManger()
        
        popManger.presentFrame = CGRect(x: 100, y: 45, width: 200, height: 300)
        return popManger
        
        
    }()
    
    
}

//extension HomeTableViewController:UIViewControllerTransitioningDelegate{
//    }
//extension HomeTableViewController: UIViewControllerAnimatedTransitioning
//{
//}